<template>
  <div style="margin-top: 20px;margin-right: 20px;margin-left: 20px">
    <!--功能区-->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>功能区</span>
      </div>
      <!--按钮-->
      <div class="search">
        <!--搜索框-->
        <el-input placeholder="请输入选题名称" prefix-icon="el-icon-search" style="width: 350px" v-model="searchKey"/>
        <!--搜索按钮-->
        <div class="search-btn">
          <el-button type="success" @click="dialogFormVisible = true">发布新课题</el-button>
          <el-button type="primary" @click="searchTopic">查 询</el-button>
          <el-button type="info" @click="clear">清 空</el-button>
        </div>
        <!--新增 / 修改 课题表单-->
        <el-dialog :title="topic.id ? '编辑' : '新增'" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
          <el-form :model="topic">
            <el-form-item label="课题名称">
              <el-input v-model="topic.title" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="面向专业/学生">
              <el-input v-model="topic.profession" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="课题来源">
              <el-input v-model="topic.source" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="课题类别">
              <el-input v-model="topic.type" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="课题完成途径">
              <el-input v-model="topic.approach" autocomplete="off"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="() => {dialogFormVisible = false; topic = {}}">取 消</el-button>
            <el-button type="primary" @click="postThesis">提 交</el-button>
          </div>
        </el-dialog>
      </div>
    </el-card>
    <!--展示已发布课题-->
    <div>
      <el-card class="box-card" style="margin-top: 20px">
        <el-table
          :data="tableData"
          style="width: 100%">
          <el-table-column prop="topicId" label="ID" v-if="false"/>
          <el-table-column prop="state" label="是否被选" :formatter="selectedState" width="80"/>
          <el-table-column prop="username" label="选题学生">
            <template slot-scope="scope">{{ scope.row.username === null ? '暂无数据' : scope.row.username }}</template>
          </el-table-column>
          <el-table-column prop="profession" label="面向专业/学生" show-overflow-tooltip/>
          <el-table-column prop="title" label="课题名称" show-overflow-tooltip min-width="120"/>
          <el-table-column prop="source" label="课题来源"/>
          <el-table-column prop="type" label="课题类别"/>
          <!--<el-table-column prop="topicAchiClass" label="成果类别" />-->
          <el-table-column prop="approach" label="课程完成途径"/>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <div v-if="scope.row.state === 0">
                <el-button
                  size="mini"
                  type="primary"
                  @click="update(scope.row)">编辑
                </el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="remove(scope.row)">删除
                </el-button>
              </div>
              <div v-else>
                <el-button
                  size="mini"
                  type="primary"
                  @click="update(scope.row)">编辑
                </el-button>
                <el-button disabled
                           size="mini"
                           type="danger"
                           @click="remove(scope.row)">删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <!--分页-->
        <div class="block">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalNum">
          </el-pagination>
        </div>
      </el-card>
    </div>
  </div>

</template>

<script>
import {
  deleteThesisApi,
  editThesisApi,
  getAllThesisApi,
  getAllThesisByTeacherIdApi,
  postNewThesisApi
} from "@/api/ThesisApi";

export default {
  name: "PublishTopic",
  data() {
    return {
      searchKey: '',
      dialogFormVisible: false,
      dialogFormTitle: "新增",
      tableData: [],
      topic: {},
      userId: this.$store.getters.GET_USERINFO.id,
      totalNum: 0,
      currentPage: 1,
      pageSize: 20,
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val;
      this.getTopic()
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getTopic()
    },
    getTopic() {
      getAllThesisByTeacherIdApi(this.userId, this.currentPage, this.pageSize).then(res => {
        this.totalNum = res.data.total
        this.tableData = res.data.records
      })
    },
    selectedState(row) {
      switch (row.state) {
        case 0:
          return <el-tag>未被选</el-tag>
        case 1:
          return <el-tag type="warning">审核中</el-tag>
        case 2:
          return <el-tag type="danger">已被选</el-tag>
        default:
          return <el-tag type="danger">未知</el-tag>
      }
    },
    remove(row) {
      if (row.state !== 0) {
        this.$alert('该课题已被学生选择，禁止删除', '提示', {
          confirmButtonText: '确定'
        })
      } else {
        let data = { id: row.id }
        deleteThesisApi(data).then(res => {
          if (res.data === true) {
            this.$message.success("删除成功")
          }
          this.getTopic()
        })
      }
    },
    postThesis() {
      if (!this.topic.id) {
        postNewThesisApi(this.topic).then(res => {
          if (res.data === true) {
            this.$message.success("操作成功")
          }
          this.topic = {}
          this.dialogFormVisible = false
          this.getTopic()
        })
      } else {
        editThesisApi(this.topic).then(res => {
          if (res.data === true) {
            this.$message.success("操作成功")
          }
          this.topic = {}
          this.dialogFormVisible = false
          this.getTopic()
        })
      }

    },
    searchTopic() {
      let data = {
        current: this.currentPage,
        size: this.pageSize,
        title: this.searchKey
      }
      getAllThesisApi(data).then(res => {
        this.totalNum = res.data.total
        this.tableData = res.data.records
      })
    },
    clear() {
      this.searchKey = ''
      this.getTopic()
    },
    update(row) {
      this.topic = {...row}
      this.dialogFormVisible = true
    }
  },
  created() {
    this.getTopic()
  }
}
</script>

<style scoped>
.block {
  margin-top: 10px;
  width: 100%;
  display: inline-block;
  text-align: center;
}

.search-btn {
  margin-left: 5px;
  display: inline-block;
  float: right;
}
</style>
